/*
*******************************************************************************
*			           constants and define declarations
*******************************************************************************
*/
/* amplitude used in QPSK */
#define D_QPSK		0.7071

/* amplitudes used in 16QAM */
#define A_16QAM		0.3162
#define A3_16QAM	0.9486

#include "../common/globaldef.h"

#include "../common/globalVai.h"

/*
int DeModulator(int M_Type, int SymbolIndex,struct Complex *Symbol , int *Dest)
{
	int i;
	int j;
	int k;
	int p_index1;// used in inserting pilot 
	int p_index2;

	struct Complex Symbol1[DATANUM];// temp 

	j = 0;// Symbol1 index 
	p_index1 = CP_Length/Pilot_Space;//temp for 12
	p_index2 = ( FFT_Size + CP_Length )/Pilot_Space;//temp for 140


	// save the data symbols in Symbol1 
	for(i = p_index1; i < (p_index2 - 1); i++)
	{
		for(k = 1; k < Pilot_Space; k++)// 7 symbols 
		{
			Symbol1[j].real = Symbol[i*Pilot_Space + k].real;
			Symbol1[j].imag = Symbol[i*Pilot_Space + k].imag;
			j++;
		}
	}
	for(i = ((p_index2 - 1) * Pilot_Space + 1); i < (FFT_Size + CP_Length) - 1; i++)// the last 6 symbols 
	{
        Symbol1[j].real = Symbol[i].real;
        Symbol1[j].imag = Symbol[j].imag;
		j++;
	}

	if(M_Type==0)// QPSK 
	{
		for(j = 0; j<DATANUM; j++)
		{
			i = SymbolIndex * DATANUM + j;

			if(Symbol1[j].real >= 0)
			{
				Dest[2*i] = 0;
			}
			else
			{
				Dest[2*i] = 1;
			}

			if(Symbol1[j].imag >= 0)
			{
				Dest[2*i+1] = 0;
			}
			else
			{
				Dest[2*i+1] = 1;
			}

		}

		return 1;

	}
	else if(M_Type==1)// 16MAM 
	{
		for(j = 0; j<DATANUM; j++)
		{
			i = SymbolIndex * DATANUM + j;

			if(Symbol1[j].real >= 0)
			{
				if(Symbol1[j].real >= 2*A_16QAM)
				{
					Dest[4*i] = 0;
					Dest[4*i+1] = 0;
				}
				else
				{
					Dest[4*i] = 1;
					Dest[4*i+1] = 0;
				}
			}
			else
			{
				if(Symbol1[j].real <= -(2*A_16QAM))
				{
					Dest[4*i] = 0;
					Dest[4*i+1] = 1;
				}
				else
				{
					Dest[4*i] = 1;
					Dest[4*i+1] = 1;
				}

			}

			if(Symbol1[j].imag >= 0)
			{
				if(Symbol1[j].imag >= 2*A_16QAM)
				{
					Dest[4*i+2] = 0;
					Dest[4*i+3] = 0;
				}
				else
				{
					Dest[4*i+2] = 1;
					Dest[4*i+3] = 0;
				}
			}
			else
			{
				if(Symbol1[j].imag <= -(2*A_16QAM))
				{
					Dest[4*i+2] = 0;
					Dest[4*i+3] = 1;
				}
				else
				{
					Dest[4*i+2] = 1;
					Dest[4*i+3] = 1;
				}
			}
		}// end of for 
		return 1;
	}
	else
	{
		return 0;
	}
}
*/

int DeModulator(int M_Type, int SymbolIndex, struct Complex *Symbol , int *Dest)
{
	int i;
	int j;
	int k;
	struct Complex Symbol1[895];// temp 

	j = 0;// Symbol1 index 

	// save the data symbols in Symbol1 
	for(i = 12; i<=138; i++)
	{
		for(k = 1; k<=7; k++)// 7 symbols 
		{
			Symbol1[j].real = Symbol[i*8 + k].real;
			Symbol1[j].imag = Symbol[i*8 + k].imag;
			j++;
		}
	}
	for(i = 139*8+1; i<=1118; i++)// the last 6 symbols 
	{
        Symbol1[j].real = Symbol[i].real;
        Symbol1[j].imag = Symbol[i].imag;
		j++;
	}

	if(M_Type==0)// QPSK 
	{
		for(j = 0; j<895; j++)
		{
			i = SymbolIndex * 895 + j;

			if(Symbol1[j].real >= 0)
			{
				Dest[2*i] = 0;
			}
			else
			{
				Dest[2*i] = 1;
			}

			if(Symbol1[j].imag >= 0)
			{
				Dest[2*i+1] = 0;
			}
			else
			{
				Dest[2*i+1] = 1;
			}

		}

		return 1;

	}
	else if(M_Type==1)// 16MAM 
	{
		for(j = 0; j<895; j++)
		{
			i = SymbolIndex * 895 + j;

			if(Symbol1[j].real >= 0)
			{
				if(Symbol1[j].real >= 2*A_16QAM)
				{
					Dest[4*i] = 0;
					Dest[4*i+1] = 0;
				}
				else
				{
					Dest[4*i] = 1;
					Dest[4*i+1] = 0;
				}
			}
			else
			{
				if(Symbol1[j].real <= -(2*A_16QAM))
				{
					Dest[4*i] = 0;
					Dest[4*i+1] = 1;
				}
				else
				{
					Dest[4*i] = 1;
					Dest[4*i+1] = 1;
				}

			}

			if(Symbol1[j].imag >= 0)
			{
				if(Symbol1[j].imag >= 2*A_16QAM)
				{
					Dest[4*i+2] = 0;
					Dest[4*i+3] = 0;
				}
				else
				{
					Dest[4*i+2] = 1;
					Dest[4*i+3] = 0;
				}
			}
			else
			{
				if(Symbol1[j].imag <= -(2*A_16QAM))
				{
					Dest[4*i+2] = 0;
					Dest[4*i+3] = 1;
				}
				else
				{
					Dest[4*i+2] = 1;
					Dest[4*i+3] = 1;
				}
			}
		}// end of for 
		return 1;
	}
	else
	{
		return 0;
	}
}

